package com.gjintel.framework.log.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.framework.constant.ActivitiMap;
import com.gjintel.framework.log.model.TSysWarningLog;
import com.gjintel.framework.log.service.IMessageService;
import com.gjintel.framework.projecttype.model.ProjectType;
import com.gjintel.framework.register.model.RegisterUser;
import com.gjintel.framework.role.service.IRoleService;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.user.service.IUserService;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author wwang
 * @description 消息提醒
 * @date 2016年9月7日
 */
@RequestMapping("/framework/message")
@Controller
@SuppressWarnings("unchecked")
public class MessageController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IRoleService roleService;

    /**
     * 获取当前登录人的所有消息提醒信息列表
     *
     * @return
     */
    @RequestMapping("/messageList")
    @ResponseBody
    public String messageList(HttpSession session, HttpServletRequest request) {
        System.err.println("每隔5分钟请求服务器，获取消息信息列表。。。。。。。ip: ");
        // 获取当前登录用户
        User user = (User) session.getAttribute("loginUser");
        String readerId = user.getId();
        RegisterUser register = userService.get(RegisterUser.class, user.getRegisterId());
        if (register != null) {//登录人为投标人
            readerId = register.getId();
        }
        TSysWarningLog warningLog = new TSysWarningLog();
        warningLog.setReadFlag(null);//未读
        warningLog.setReaderId(readerId);
        List<TSysWarningLog> warningLogList = this.messageService.findAllByIds(warningLog, null, null);
        return JSONArray.fromObject(warningLogList).toString();
    }


    /**
     * 消息提醒信息修改(已读操作)
     *
     * @return
     */
    @RequestMapping("/messageEdit")
    @ResponseBody
    public boolean compettipsedit(String id) {
        TSysWarningLog warningLog = this.messageService.get(TSysWarningLog.class, id);
        warningLog.setReadFlag(1);
        this.messageService.saveOrUpdate(warningLog);
        return true;
    }

    /**
     * @throws ParseException
     * @description 消息列表
     * @author shy
     */
    @RequestMapping("/getMessageList")
    @ResponseBody
    public void messageList(HttpSession session, HttpServletResponse resp, TSysWarningLog tWarningLog, Page page, java.sql.Date startTime, java.sql.Date endTime, Integer flag) throws ParseException {
        User user = (User) session.getAttribute("loginUser");
        endTime = StringUtil.addOneDayDate(endTime, 1);
        User u = new User();
        u.setId(user.getId());
        RegisterUser register = userService.get(RegisterUser.class, user.getRegisterId());
        if (register != null) {// 登录人为投标人
            u.setId(register.getId());
        }
        this.messageService.getMessageList(tWarningLog, page, u, startTime, endTime, flag);
        JSONObject jsonObject = CommUtil.getPageJSONData(page, true);
        try {
            resp.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * @description 消息标记已读
     * @author shy
     */
    @RequestMapping("/readed")
    @ResponseBody
    public void readed(String ids) {
        this.messageService.readed(ids);
    }

    /**
     * @description 消息标记已读
     * @author shy
     */
    @RequestMapping("/getMessageCount")
    @ResponseBody
    public String getMessageCount(HttpSession session) {
        // 获取当前登录用户
        User user = (User) session.getAttribute("loginUser");
        String readerId = user.getId();
        RegisterUser register = userService.get(RegisterUser.class, user.getRegisterId());
        if (register != null) {// 登录人为投标人
            readerId = register.getId();
        }
        int count = this.messageService.getCountByReaderId(readerId);
        return count + "";
    }


    /**
     * 消息列表页面
     *
     * @param session
     * @return
     */
    @RequestMapping("/message")
    public ModelAndView message(HttpSession session) {
        ModelAndView mv = new ModelAndView("/framework/log/message-list");
        User user = (User) session.getAttribute("loginUser");
        // 判断是否为信息管理员，显示到处项目按钮
        String roleIds = ActivitiMap.MESSAGER_ID;
        //boolean isShowCreateButton = roleService.isPrivilege(user.getId(), roleIds);
        boolean isShowCreateButton = false;
        mv.addObject("isShowExportBtn", isShowCreateButton);
        return mv;
    }

}
